package com.huanwei.login_regist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.newbean.KeCheng;
import com.huanwei.cc.newbean.LoginT;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *我要选课页面
 */
public class MyXuanKe_Activity extends AppCompatActivity {
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.spinner_shijianduan)
    MaterialSpinner spinnerShijianduan;

    @BindView(R.id.but_regist)
    Button butRegist;
    @BindView(R.id.tv_xingqi)
    TextView tvXingqi;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_laoshi)
    TextView tvLaoshi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myxuanke);
        ButterKnife.bind(this);
        initview();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getCurriculums();
    }

    String kecheng;
    List<KeCheng.DataBean> ccList;
    String week;
    String Shijianduan;
    String Address;
    String Teacher_name;

    private void initview() {


        spinnerShijianduan.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                kecheng = teachers.get(position);
                week = ccList.get(position).getWeek();
                Shijianduan = ccList.get(position).getShijianduan();
                Address = ccList.get(position).getAddress();
                Teacher_name = ccList.get(position).getTeacher_name();
                tvXingqi.setText(week);
                tvTime.setText(Shijianduan);
                tvAddress.setText(Address);
                tvLaoshi.setText(Teacher_name);

            }
        });

    }

    @OnClick({R.id.img_back, R.id.but_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_regist:

//                String shangkeName = edName.getText().toString();
//                if (TextUtils.isEmpty(shangkeName)) {
//                    Toast.makeText(this, "请输入上课地址", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                String Kechengname = editKechengname.getText().toString();
//                if (TextUtils.isEmpty(Kechengname)) {
//                    Toast.makeText(this, "请输入课程名称", Toast.LENGTH_SHORT).show();
//                    return;
//                }


                API.addmyCurriculum(kecheng, Teacher_name, week, Shijianduan, Address, "", "", new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("111", "xx" + st);


                        LoginT login = JSONUtils.parseJSON(st, LoginT.class);
                        if (login.isIssuccess() == true) {
                            Toast.makeText(MyXuanKe_Activity.this, "添加课程成功！", Toast.LENGTH_SHORT).show();

//                            finish();
                        } else {
                            Toast.makeText(MyXuanKe_Activity.this, login.getMsg(), Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(MyXuanKe_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }
                });


                break;
        }
    }

    List<String> teachers = new ArrayList<String>();

    public void getCurriculums() {
        API.getCurriculums(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);


                KeCheng teacher = JSONUtils.parseJSON(st, KeCheng.class);
                if (teacher.isIssuccess() == true) {

                    ccList = teacher.getData();

                    for (int i = 0; i < ccList.size(); i++) {
                        teachers.add(ccList.get(i).getSubject_name());
                    }
                    kecheng = teachers.get(0);
                    week = ccList.get(0).getWeek();
                    Shijianduan = ccList.get(0).getShijianduan();
                    Address = ccList.get(0).getAddress();
                    Teacher_name = ccList.get(0).getTeacher_name();
                    tvXingqi.setText(week);
                    tvTime.setText(Shijianduan);
                    tvAddress.setText(Address);
                    tvLaoshi.setText(Teacher_name);
                    spinnerShijianduan.setItems(teachers);

                } else {
                    Toast.makeText(MyXuanKe_Activity.this, teacher.getMsg(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(MyXuanKe_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
